package com.mokylin.dbschema.config;

import com.mokylin.dbschema.handler.SqlVersionHandlerFactory;
import com.mokylin.dbschema.util.EnvConfigUtils;
import com.mokylin.dbschema.util.StringUtils;

import java.util.Objects;

/**
 * @author lizhao  2014/9/17.
 */

public class ConfigInfo {

    /**
     * 当前项目标识，通过此标识去配置表获取配置信息
     */
    private static String APP_ID;

    /**
     * sql文件编码
     */
    private static String FILE_CHARSET;

    /**
     * 数据库地址
     */
    private static  String DB_HOST;

    /**
     * 数据库端口
     */
    private static  String DB_PORT;

    private static  String DB_NAME;

    private static  String DB_USERNAME;

    private static  String DB_PASSWORD;

    private static  String JDBC_DRIVER;

    /**
     * sql文件所在文件夹，相对classpath路径或者绝对路径
     */
    private static  String SQL_DIR;

    /**
     * sql版本获取处理类，默认 com.mokylin.dbschema.handler.DefaultSqlVersionHandlerImpl
     */
    private static String SQL_VERSION_HANDLER;

    /**
     *最近的版本号，不指定则为数据库最新版本号，指定后以指定为准
     */
    private static String NEWEST_VERSION;

    /**
     * 配置前缀，如果指定则在所有key前加上次前缀
     */
    private static String PREFIX="";

    /**
     * 数据库连接的心跳时间，如果超出此时间则认为升级完成或者服务器已挂掉
     */
    private static long DB_CONNECT_HEARTBEAT_SECONDS;

    /**
     * 重试次数，如果当前数据库状态错误会进行重试
     */
    private static int ILLEGAL_STATE_RETRY_NUM;

    private static String CONFIG_DB_HOST;
    private static  String CONFIG_DB_PORT;
    private static  String CONFIG_DB_USERNAME;
    private static  String CONFIG_DB_PASSWORD;
    private static  String CONFIG_DB_NAME;

    public static String getConfigDbHost() {
        return CONFIG_DB_HOST==null? EnvConfigUtils.getString(PREFIX + ConfigKey.CONFIG_DB_HOST) : CONFIG_DB_HOST;
    }

    public static void setConfigDbHost(String configDbHost) {
        CONFIG_DB_HOST = configDbHost;
    }

    public static String getConfigDbPort() {
        return CONFIG_DB_PORT==null? EnvConfigUtils.getString(PREFIX + ConfigKey.CONFIG_DB_PORT) : CONFIG_DB_PORT;
    }

    public static void setConfigDbPort(String configDbPort) {
        CONFIG_DB_PORT = configDbPort;
    }

    public static String getConfigDbUsername() {
        return CONFIG_DB_USERNAME==null? EnvConfigUtils.getString(PREFIX + ConfigKey.CONFIG_DB_USERNAME) : CONFIG_DB_USERNAME;
    }

    public static void setConfigDbUsername(String configDbUsername) {
        CONFIG_DB_USERNAME = configDbUsername;
    }

    public static String getConfigDbPassword() {
        return CONFIG_DB_PASSWORD==null? EnvConfigUtils.getString(PREFIX + ConfigKey.CONFIG_DB_PASSWORD) : CONFIG_DB_PASSWORD;
    }

    public static void setConfigDbPassword(String configDbPassword) {
        CONFIG_DB_PASSWORD = configDbPassword;
    }

    public static String getConfigDbName() {
        return CONFIG_DB_NAME==null? EnvConfigUtils.getString(PREFIX + ConfigKey.CONFIG_DB_NAME) : CONFIG_DB_NAME;
    }

    public static void setConfigDbName(String configDbName) {
        CONFIG_DB_NAME = configDbName;
    }

    /**
     * 默认缓存失效时间（秒）
     */
    public static int getDefaultCacheExpireSeconds() {
        return 60;
    }

    public static String getAppId() {
        return APP_ID==null? EnvConfigUtils.getString(PREFIX + ConfigKey.APP_ID) : APP_ID;
    }

    public static void setAppId(String appId) {
        ConfigInfo.APP_ID = appId;
    }

    public static int getIllegalStateRetryNum() {
        return ILLEGAL_STATE_RETRY_NUM!=0?ILLEGAL_STATE_RETRY_NUM:EnvConfigUtils.getInt(PREFIX + ConfigKey.ILLEGAL_STATE_RETRY_NUM, 10);
    }

    public static void setIllegalStateRetryNum(int illegalStateRetryNum) {
        ILLEGAL_STATE_RETRY_NUM = illegalStateRetryNum;
    }

    public static long getDbConnectHeartbeatSeconds() {
        return DB_CONNECT_HEARTBEAT_SECONDS!=0?DB_CONNECT_HEARTBEAT_SECONDS:EnvConfigUtils.getLong(PREFIX + ConfigKey.DB_CONNECT_HEARTBEAT_SECONDS, 60L);
    }

    public static void setDbConnectHeartbeatSeconds(long dbConnectHeartbeatSeconds) {
        DB_CONNECT_HEARTBEAT_SECONDS = dbConnectHeartbeatSeconds;
    }

    public static String getFileCharset() {
        return StringUtils.isNotBlank(FILE_CHARSET)?FILE_CHARSET:EnvConfigUtils.getString(PREFIX+ConfigKey.FILE_CHARSET,"UTF-8");
    }

    public static void setFileCharset(String fileCharset) {
        FILE_CHARSET = fileCharset;
    }

    public static String getNewestVersion() {
        return NEWEST_VERSION==null?(NEWEST_VERSION=SqlVersionHandlerFactory.getSqlVersionHandler().getCurrentVersion()):NEWEST_VERSION;
    }


    public static String getPREFIX() {
        return PREFIX;
    }

    public static void setPREFIX(String PREFIX) {
        ConfigInfo.PREFIX = Objects.toString(PREFIX,"");
    }


    public static String getDbHost() {
        return StringUtils.isBlank(DB_HOST)? EnvConfigUtils.getString(PREFIX+ConfigKey.DB_HOST):DB_HOST;
    }

    public static void setDbHost(String dbHost) {
        DB_HOST = dbHost;
    }

    public static String getDbPort() {
        return StringUtils.isBlank(DB_PORT)?EnvConfigUtils.getString(PREFIX+ConfigKey.DB_PORT):DB_PORT;
    }

    public static void setDbPort(String dbPort) {
        DB_PORT = dbPort;
    }

    public static String getDbName() {
        return StringUtils.isBlank(DB_NAME)?EnvConfigUtils.getString(PREFIX+ConfigKey.DB_NAME):DB_NAME;
    }

    public static void setDbName(String dbName) {
        DB_NAME = dbName;
    }

    public static String getDbUsername() {
        return StringUtils.isBlank(DB_USERNAME)?EnvConfigUtils.getString(PREFIX+ConfigKey.DB_USERNAME):DB_USERNAME;
    }

    public static void setDbUsername(String dbUsername) {
        DB_USERNAME = dbUsername;
    }

    public static String getDbPassword() {
        return StringUtils.isBlank(DB_PASSWORD)?EnvConfigUtils.getString(PREFIX+ConfigKey.DB_PASSWORD):DB_PASSWORD;
    }

    public static void setDbPassword(String dbPassword) {
        DB_PASSWORD = dbPassword;
    }

    public static String getJdbcDriver() {
        return StringUtils.isBlank(JDBC_DRIVER)?EnvConfigUtils.getString(PREFIX+ConfigKey.JDBC_DRIVER, "com.mysql.jdbc.Driver"):JDBC_DRIVER;
    }

    public static void setJdbcDriver(String jdbcDriver) {
        JDBC_DRIVER = jdbcDriver;
    }

    public static String getSqlDir() {
        return StringUtils.isBlank(SQL_DIR)? EnvConfigUtils.getString(PREFIX+ConfigKey.SQL_DIR, "sqls"):SQL_DIR;
    }

    public static void setSqlDir(String sqlDir) {
        SQL_DIR = sqlDir;
    }

    public static String getSqlVersionHandler() {
        return StringUtils.isBlank(SQL_VERSION_HANDLER)?EnvConfigUtils.getString(PREFIX+ConfigKey.SQL_VERSION_HANDLER,"com.mokylin.dbschema.handler.DefaultSqlVersionHandlerImpl"):SQL_VERSION_HANDLER;
    }

    public static void setSqlVersionHandler(String sqlVersionHandler) {
        SQL_VERSION_HANDLER = sqlVersionHandler;
    }
}